package com.glut.gultguy.mq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.glut.gultguy.entity.Goods;
import com.glut.gultguy.mapper.GoodsMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class TestDemoXXl {
    private List<Goods> goods ;
    private List<Goods> downGoods ;
    private List<Goods> examineGoods;


    @Autowired(required = false)
    private GoodsMapper goodsMapper;


    @Autowired
    private Producer producer;

    @XxlJob("upGoods")
    public void upGoods()throws Exception{
       Date date = new Date();
       if (goods.size()>0){
           if (goods.get(0).getStart_time().getTime()<=date.getTime()){
               System.out.println("up ok!");
               goods.get(0).setGoodsState(2);
               goodsMapper.updateById(goods.get(0));
               System.out.println("up 前"+goods.size());
               goods.remove(0);
               System.out.println("up 后"+goods.size());
           }
       }
    }

    @XxlJob("getUpGoods")
    public void getGoods()throws Exception{
        Date date = new Date();
        date.setTime(date.getTime() + 1000*60*60);
        goods = goodsMapper.selectList(new QueryWrapper<Goods>().eq("goodsState",1).le("start_time",date).ge("start_time",new Date()).orderByAsc("start_time"));
        System.out.println("get up OK!");
        System.out.println(goods.size());
    }

    @XxlJob("getDownGoods")
    public void getDownGoods()throws Exception{
        Date date = new Date();
        date.setTime(date.getTime() + 1000*60*60);
        downGoods = goodsMapper.selectList(new QueryWrapper<Goods>().eq("goodsState",2).ge("end_time",new Date()).le("end_time",date).orderByAsc("end_time"));
        System.out.println("get end OK!");
    }

    @XxlJob("downGoods")
    public void downGoods()throws Exception{
        Date date = new Date();
        if (downGoods.size()>0){
            if (downGoods.get(0).getEnd_time().getTime()<=date.getTime()){
                System.out.println("down ok!");
                System.out.println(downGoods.get(0));
                System.out.println(downGoods.get(0));
                producer.sendMessage(downGoods.get(0).getId());
                downGoods.remove(0);
            }
        }
    }
    @XxlJob("examine")
   public void examine()throws Exception {
        Date date = new Date();

        if (examineGoods.size()>0){

            if ((examineGoods.get(0).getAddTime().getTime()+1000*60*60)<=date.getTime()){
                System.out.println("审核通过");
                examineGoods.get(0).setGoodsState(1);
                goodsMapper.updateById(examineGoods.get(0));
                examineGoods.remove(0);

            }
        }
   }

    @XxlJob("getExamine")
    public void getExamine()throws Exception {
        Date date = new Date();
        date.setTime(date.getTime() + 1000*60*60);
        examineGoods = goodsMapper.selectList(new QueryWrapper<Goods>().eq("goodsState",0).le("addTime",date).orderByAsc("addTime"));
        System.out.println("get examine OK!");
        System.out.println("size:"+examineGoods.size());
    }

}
